import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.widgets.Canvas;


public class ArmAddition extends AbstractRobotDecorator{
	
	private boolean isCarrying = false;
	private Box box;
	private int len; //length of half arm line
	
	//c'tor
	ArmAddition(Robot robot)
	{
		super(robot);
		//calculate the size half of the arm's length
		len = (int)(Math.round(Math.sqrt(200)) / 2);
	}
	
	//returns true if picked up box and false if didn't
	public  boolean pickupBox(Box box){
		//if is able to pick up box
		if( (box.getPosition().isSame(robot.getCurrentPosition()))
					&& box.isDown() && !isCarrying){
			box.setToUp();
			this.box = box;
			isCarrying = true;
			return true;
		}
		return false;
	}

	//returns true if put down box and false if didn't
	public  boolean putdownBox(){
		if(isCarrying){
			box.setToDown();
			isCarrying = false;
			return true;
		}
		return false;
	}
	//set robots heading and speed
	public void move(int speed, int heading) {robot.move(speed,heading);}
	//move robot
	public void act(){
		robot.act();
		//set box's position if robot is holding box
		if(isCarrying){
			box.setPosition(robot.getCurrentPosition());
		}
	}
	
	//paints robot to canvas
	public  void paint(PaintEvent e){
		robot.paint(e);
		
		//draw arm
		Canvas c = (Canvas)e.widget; // get the canvas
		int maxX = c.getSize().x;	 // max size
		int maxY = c.getSize().y;
		
		int mx = maxX/2,my = maxY/2;// mid point as (0,0)
		int myX = mx + robot.getCurrentPosition().x;	// set the current position
		int myY = my - robot.getCurrentPosition().y;	// set the current position
		
		//if robot isn't carrying box - draw lines
		if(!isCarrying){
			e.gc.drawLine(myX - len, myY - len, myX - len, myY + len); //left vertical
			e.gc.drawLine(myX - len, myY + len, myX + len, myY + len); //bottom horizontal 
			e.gc.drawLine(myX + len, myY + len, myX + len, myY - len); //right vertical
		}
		else{
			e.gc.drawString(box.getName(),myX+13, myY+26,true);
			box.paint(e);
		}
	}
}
